package com.huatai.exchange.bootup.controller;

import com.faneui.bootup.controller.AbstractRestController;
import com.faneui.common.bean.FaneuiBaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.huatai.exchange.facade.DataFileExchangeController;
import com.huatai.exchange.facade.ExchangeExcuteController;
import com.huatai.exchange.service.ExcExchangeInfoService;
import com.huatai.exchange.service.ExcuteExchangeService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping(value = "execute")
@Api("数据文件交换执行")
public class ExchangeExcuteControllerImpl extends AbstractRestController implements ExchangeExcuteController {
    @Autowired
    private ExcuteExchangeService excuteExchangeService;

    @RequestMapping(value = "/{exchangeInfoId}", method = RequestMethod.GET)
    @ResponseBody
    public FaneuiBaseResp excuteExchange(@PathVariable String exchangeInfoId ,@RequestParam(required = false) String occurDate) throws BusinessException {
        FaneuiBaseResp resp = new FaneuiBaseResp();
        try{
            if(StringUtils.isBlank(occurDate)) {
                occurDate = DateFormatUtils.yyyyMMdd.format(new Date()) ;
            }
            excuteExchangeService.executeExchangeDataFile(exchangeInfoId,occurDate);
        }catch (Exception ex) {
            logger.info("",ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

}
